package base.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.shizhefei.mvc.IAsyncDataSource;
import com.shizhefei.mvc.ILoadViewFactory;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;
import com.shizhefei.mvc.RequestHandle;
import com.shizhefei.mvc.ResponseSender;
import com.shizhefei.recyclerview.HFAdapter;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.PullRecyAdpater;
import base.BaseBean;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import utils.NetUtils;
import utils.pull.LoadViewFactory;
import widget.RecyViewHolder;

public abstract class BaseSwipeFragment<T> extends BaseNetFragment implements IAsyncDataSource<List<T>> {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private MVCHelper<List<T>> mListMVCHelper;
    private NetUtils mNetUtils;
    private Gson mGson;
    private SwipeAdapter mSwipeAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNetUtils = new NetUtils(context);
        mNetUtils.initHeader();
        mGson = new Gson();
    }

    @Override

    public void success(String action, BaseBean baseBean) {

    }

    @Override
    public void error(String action, Throwable e) {

    }

    public void refresh() {
        mListMVCHelper.refresh();
    }

    public void loadMore() {
        mListMVCHelper.loadMore();
    }

    @Override
    protected void findViews() {
        mSwipeAdapter = new SwipeAdapter(getItemLayoutResId());
    }

    @Override
    protected void formatData() {
        mSwipeRefreshLayout = getSwipeRefreshLayout();
        mListMVCHelper = new MVCSwipeRefreshHelper<List<T>>(mSwipeRefreshLayout, setLoadView(), setLoadMoreView());
        mListMVCHelper.setDataSource(this);
        mListMVCHelper.setAdapter(mSwipeAdapter);
        mSwipeAdapter.setOnItemClickListener(new HFAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(HFAdapter adapter, RecyclerView.ViewHolder vh, int position) {
                onItemClickListener(mSwipeAdapter, vh, position);
            }
        });
    }

    public ILoadViewFactory.ILoadMoreView setLoadMoreView() {
        return new LoadViewFactory().madeLoadMoreView();
    }

    public ILoadViewFactory.ILoadView setLoadView() {
        return new LoadViewFactory().madeLoadView();
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {

    }

    public SwipeAdapter getSwipeAdapter() {
        return mSwipeAdapter;
    }

    public abstract SwipeRefreshLayout getSwipeRefreshLayout();

    public abstract String getHttpUrl();

    public abstract Map<String, Object> getMapParam();

    public abstract SwipeData formatHttpData(boolean isRefresh, BaseBean bean);

    public abstract int getItemLayoutResId();

    public abstract void viewConvert(BaseViewHolder helper, T item);

    public void onItemClickListener(SwipeAdapter adapter, RecyclerView.ViewHolder vh, int position) {

    }


    @Override
    public RequestHandle refresh(final ResponseSender<List<T>> sender) throws Exception {
        currentPage = 1;
        return loadData(true, sender, currentPage);
    }

    @Override
    public RequestHandle loadMore(ResponseSender<List<T>> sender) throws Exception {
        return loadData(false, sender, currentPage + 1);
    }

    @Override
    public boolean hasMore() {
        return currentPage < maxPage;
    }

    private int currentPage = 1;
    private int maxPage = 1;

    private RequestHandle loadData(final boolean isRefresh, final ResponseSender<List<T>> sender, final int page) {
        Map<String, Object> map = getMapParam();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put("page", page);
        mNetUtils.post(getHttpUrl(), map, new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String responseString = responseBody.string();
                    SwipeData swipeData = formatHttpData(isRefresh, mGson.fromJson(responseString, BaseBean.class));
                    maxPage = swipeData.maxPage;
                    currentPage = swipeData.currentPage;
                    sender.sendData(swipeData.list);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        return null;
    }

    public class SwipeData {
        public List<T> list;
        public int currentPage = 1;
        public int maxPage = 1;
    }

    public class SwipeAdapter extends PullRecyAdpater<T> {

        public SwipeAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(RecyViewHolder holder, T item) {
            viewConvert(holder, item);
        }
    }
}